package com.zxw.third.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.entity.baidu.BaiduHotEntity;
import com.zxw.common.entity.baidu.BaiduHotListQuery;
import com.zxw.common.entity.baidu.BaiduHotQuery;
import com.zxw.common.entity.baidu.BaiduHotVO;
import com.zxw.common.utils.HotUtil;
import com.zxw.third.mapper.BaiduHotMapper;
import com.zxw.third.service.BaiduHotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-10-11
 */
@Service
@Slf4j
public class BaiduHotServiceImpl extends ServiceImpl<BaiduHotMapper, BaiduHotEntity> implements BaiduHotService {


    /**
     * 添加
     *
     * @param baiduHotQuery BaiduHotQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(BaiduHotQuery baiduHotQuery) {
        BaiduHotEntity baiduHotEntity = new BaiduHotEntity();
        BeanUtils.copyProperties(baiduHotQuery, baiduHotEntity);
        return super.save(baiduHotEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<BaiduHotQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<BaiduHotQuery> list) {
        List<BaiduHotEntity> collect = list.stream().map(item -> {
            BaiduHotEntity baiduHotEntity = new BaiduHotEntity();
            BeanUtils.copyProperties(item, baiduHotEntity);
            return baiduHotEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param baiduHotQuery BaiduHotQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(BaiduHotQuery baiduHotQuery) {
        BaiduHotEntity baiduHotEntity = new BaiduHotEntity();
        BeanUtils.copyProperties(baiduHotQuery, baiduHotEntity);
        baiduHotEntity.setId(null);
        baiduHotEntity.setStatus("1");
        return super.updateById(baiduHotEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return BaiduHotDO
     */
    @Override
    public BaiduHotEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param baiduHotListQuery BaiduHotListQuery
     * @return IPage<BaiduHotDO>
     */
    @Override
    public IPage<BaiduHotEntity> list(BaiduHotListQuery baiduHotListQuery) {
        IPage<BaiduHotEntity> pageInfo = new Page<>(baiduHotListQuery.getCurrent(), baiduHotListQuery.getSize());
        QueryWrapper<BaiduHotEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaiduHotEntity::getStatus, 1)
                .orderByDesc(BaiduHotEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public List<BaiduHotVO> newsHotList() {
        QueryWrapper<BaiduHotEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BaiduHotEntity> lambda = queryWrapper.lambda();
        lambda.orderByDesc(BaiduHotEntity::getCreateTime);
        lambda.last("limit 30");
        List<BaiduHotEntity> list = super.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list.stream().map(e -> {
                BaiduHotVO baiduHotVO = new BaiduHotVO();
                BeanUtils.copyProperties(e, baiduHotVO);
                return baiduHotVO;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public void newsSave() {
        List<BaiduHotVO> baiduHotVOList = HotUtil.grabBaiduHotNewsJson();
        if (CollUtil.isNotEmpty(baiduHotVOList)) {
            List<BaiduHotQuery> baiduHotQueryList = baiduHotVOList.stream().map(e -> {
                BaiduHotQuery query = new BaiduHotQuery();
                BeanUtils.copyProperties(e, query);
                return query;
            }).collect(Collectors.toList());
            this.saveBatch(baiduHotQueryList);
        }
    }

}
